package ch.zbw.pra.sysventory.test.domain.dbhandling;

import static org.junit.Assert.assertTrue;

import java.sql.ResultSet;
import java.sql.SQLException;

import org.junit.After;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;

import ch.zbw.pra.sysventory.domain.dbconnector.DBConnectorCreator;
import ch.zbw.pra.sysventory.domain.dbconnector.MySQLConnector;

@Ignore("Nur verwenden, wenn die ganze DB gelöscht werden soll!")
public class DeleteAllDataInDB {

	DBConnectorCreator dbConnectorCreator;
	MySQLConnector mySQLConnector;

	@Before
	public void setup() {
		this.dbConnectorCreator = new DBConnectorCreator();
		this.mySQLConnector = (MySQLConnector) this.dbConnectorCreator.createConnector();
		this.mySQLConnector.connect();
	}

	@After
	public void teardown() {
		this.mySQLConnector.closeConnection();
	}

	@Test
	public void deleteComputerHasNicsTable() {
		assertTrue(deleteTableData("computer_has_nic"));
	}

	@Test
	public void deleteComputerHasPrinterTable() {
		assertTrue(deleteTableData("computer_has_printer"));
	}

	@Test
	public void deleteComputerHasProzessorTable() {
		assertTrue(deleteTableData("computer_has_processor"));
	}

	@Test
	public void deleteComputerHasSoftwareTable() {
		assertTrue(deleteTableData("computer_has_software"));
	}

	@Test
	public void deleteSoftwareHasVendorTable() {
		assertTrue(deleteTableData("software_has_vendorSoftware"));
	}

	@Test
	public void deleteVendorSoftwareTable() {
		assertTrue(deleteTableData("vendorSoftware"));
	}

	@Test
	public void deleteSoftwareTable() {
		assertTrue(deleteTableData("software"));
	}

	@Test
	public void deleteNicTable() {
		assertTrue(deleteTableData("nic"));
	}

	@Test
	public void deletePrinterTable() {
		assertTrue(deleteTableData("printer"));
	}

	@Test
	public void deleteProcessorTable() {
		assertTrue(deleteTableData("processor"));
	}

	@Test
	public void deleteScanversionHasComputerTable() {
		assertTrue(deleteTableData("scanversion_has_computer"));
	}

	@Test
	public void deleteComputerTable() {
		assertTrue(deleteTableData("computer"));
	}

	@Test
	public void deleteOperatingSystemTable() {
		assertTrue(deleteTableData("operatingSystem"));
	}

	@Test
	public void deleteScanversionTable() {
		assertTrue(deleteTableData("scanversion"));
	}

	public boolean deleteTableData(String table) {
		String query = "DELETE FROM " + table + ";";
		this.mySQLConnector.updateQuery(query);
		boolean isTableEmpty = isTableEmpty(table);
		return isTableEmpty;
	}

	public boolean isTableEmpty(String table) {
		ResultSet rs = this.mySQLConnector.executeQuery("SELECT * FROM " + table + ";");
		boolean isTableEmpty = true;
		try {
			while (rs.next()) {
				isTableEmpty = false;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return isTableEmpty;
	}
}
